package com.ybdz.scanner.activity;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;

import com.ybdz.scanner.R;
import com.ybdz.scanner.api.Constant;
import com.ybdz.scanner.base.BaseActivity;
import com.ybdz.scanner.event.ObjEvent;
import com.ybdz.scanner.receiver.ScannerInterface;
import com.ybdz.scanner.utils.MyApplication;
import com.ybdz.scanner.utils.MyToast;
import com.ybdz.scanner.utils.UIUtils;

import org.greenrobot.eventbus.EventBus;

import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * @ClassName: QueryScanActivity
 * @author: zhao
 * @Description: 查询扫一扫界面
 * @date 2018/2/24 16:04
 */
public class QueryScanActivity extends BaseActivity {

    private boolean isContinue = false;        //连续扫描的标志
    private TakeResultReceiver mResultReceiver;
    private ScannerInterface mScanner;
    private static final String RES_ACTION = "android.intent.action.SCANRESULT";

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_query);
        ButterKnife.bind(this);

    }

    @Override
    protected void onResume() {
        super.onResume();
        mResultReceiver = new TakeResultReceiver();
        mScanner = initScanner(mResultReceiver);
    }

    @OnClick({R.id.ll_back, R.id.home, R.id.bt_scan})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.ll_back:                  //返回
                finish();
                break;
            case R.id.home:                     //返回首页
                startActivity(new Intent(QueryScanActivity.this,MainActivity.class));
                finish();
                break;
            case R.id.bt_scan:                  //开启扫描
                mScanner.scan_start();
                MyApplication.setScannerClick(Constant.Query);
                break;
        }
    }

    /**
     * 指定139的物理按键（中间黄色按键）按下来出发扫描
     */
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == 139 && event.getRepeatCount() == 0) {
            mScanner.scan_start();
            MyApplication.setScannerClick(Constant.Query);
        }
        return super.onKeyDown(keyCode, event);
    }

    public boolean onKeyUp(int keyCode, KeyEvent event) {   //按键弹起，停止扫描
        if (keyCode == 139) {
            mScanner.scan_stop();
        } else if (keyCode == 140) {
            mScanner.scan_stop();

            isContinue = !isContinue;
            if (isContinue) {
                mScanner.continceScan(true);
            } else {
                mScanner.continceScan(false);
            }
        }
        return super.onKeyUp(keyCode, event);
    }


    /**
     * 接收扫描结果
     */
    private class TakeResultReceiver extends BroadcastReceiver {

        @Override
        public void onReceive(Context context, Intent intent) {
            final String scanResult = intent.getStringExtra("value");
            Log.d("scanResult:",scanResult);
            //如果条码长度大于0，解码成功。如果解码长度等于0，解码失败。
            if (intent.getAction().equals(RES_ACTION)) {
                //获取扫描结果
                if (scanResult.length() > 0 && MyApplication.getScannerClick().equals(Constant.Query)) {
                    //解码成功
                    if (scanResult.contains(Constant.Ininventoryitem)) {
                        String[] split = scanResult.split(Constant.Sprit);
                        if (split[split.length - 2].equals(Constant.Ininventoryitem)) {
                            String result = split[split.length - 1];
                            EventBus.getDefault().postSticky(new ObjEvent("queryResult",result));
                            startActivity(new Intent(QueryScanActivity.this,QueryDetailsActivity.class));
//                            QueryScanActivity.this.finish();
                        } else {
                            MyToast.show(QueryScanActivity.this, "未查询到产品信息，请重新扫码。");
                        }
                    } else {
                        MyToast.show(QueryScanActivity.this, "未查询到产品信息，请重新扫码。");
                    }


                } else {
                    //扫码失败
                }
            }
        }
    }

    @Override
    protected void onPause() {
        super.onPause();
        finishScanner(mResultReceiver);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();


    }
}
